<?php
namespace app\admins\controller;

use think\facade\Db;

class orders extends Base
{
    // 订单列表
    public function index() {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            // 搜索
            $map = [];
            if (!empty($param['search'])) {
                foreach ((array)$param['search'] as $k => $v) {
                    if ($v) $map[] = [$k, 'like', "%{$v}%"];
                }
            }
            // 查询
            $list = Db::table('orders')->order('id', 'desc')->where($map);
            $data['count'] = $list->count();
            if ($data['count']) {
                $data['code'] = 0;
            } else {
                $data['code'] = 1;
                $data['msg'] = '暂无记录';
            }
            // 分页
            $list = $list->page($param['page'], $param['limit']);
            // 渲染
            $orders_products = Db::table('orders_product')->whereIn('ord_no', $list->column('ord_no'))->select();
            $product_skus = Db::table('product_sku')->whereIn('out_no', $orders_products->column('sku_out_no'))->select();
            $users = Db::table('users')->whereIn('id', $list->column('uid'))->select();
            $data['data'] = $list->select()->map(function ($item) use ($product_skus, $orders_products, $users) {
                $item['products'] = $orders_products->filter(function ($el) use ($item) {
                    return $el['ord_no'] == $item['ord_no'];
                });
                $item['products'] = $item['products']->map(function($ele) use ($product_skus) {
                    $ele['sku'] = $product_skus->filter(function($elem) use ($ele) {
                        return $elem['out_no'] == $ele['sku_out_no'];
                    })->shift();
                    return $ele;
                });
                $item['product'] = $item['products']->reduce(function($res, $next){
                    $res .= '<p>商品名称：'. $next['sku']['sku_title'] .'数量：'. $next['count'] .'，单价：'. $next['price'] .'</p>';
                    return $res;
                }, '');
                $item['uphone'] = $users->column('phone', 'id')[$item['uid']];
                $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
                $status_text = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
                $item['status_text'] = $status_text[$item['status']];
                return $item;
            });
            exit(json_encode($data));
        }
        return view('/orders/index');
    }

    // 订单修改
    public function edit() {
        $id = (int)input('get.id');
        $data['row'] = Db::table('orders')->where('id', $id)->find();
        !$data['row'] && exit('<p style="text-align:center">数据不存在</p>');
        $data['status_select'] = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
        return view('/orders/edit', $data);
    }

    // 订单保存修改
    public function save_edit() {
        $id = input('post.id');
        $data = $this->request->only(['money', 'express_no', 'express_name', 'status']);
        $data['status'] = (int)$data['status'];
        // 验证
        $validate = $this->app->validate->rule(['money|金额' => 'require|float']);
        !$validate->check($data) && exit(json_encode(['code' => 1, 'msg' => $validate->getError()]));
        Db::table('orders')->where('id', $id)->update($data);
        exit(json_encode(['code' => 0, 'msg' => '保存成功']));
    }

    // 订单删除
    public function del() {
        $id = input('post.id');
        $item = Db::table('orders')->whereIn('id', $id)->select();
        $item->isEmpty() && exit(json_encode(['code' => 1, 'msg' => '数据错误']));
        Db::table('orders_product')->whereIn('ord_no', $item->column('ord_no'))->delete();
        Db::table('orders')->whereIn('id', $item->column('id'))->delete();
        exit(json_encode(['code' => 0, 'msg' => '删除成功']));
    }

    // 订单商品详情
    public function orders_product() {
        $id = (int)input('get.id');
        $row = Db::table('orders')->where('id', $id)->find();
        !$row && exit('<p style="text-align:center">数据不存在</p>');
        $lists = Db::table('orders_product')->whereIn('ord_no', $row['ord_no'])->select();
        $product_skus = Db::table('product_sku')->whereIn('out_no', $lists->column('sku_out_no'))->select();
        $data['lists'] = $lists->map(function($list) use($product_skus) {
            $list['sku_title'] = $product_skus->column('sku_title', 'out_no')[$list['sku_out_no']];
            return $list;
        });
        return view('/orders/orders_product', $data);
    }
}